/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Common.GeneralHandling;
import POJO.StatusPOJO;
import POJO.UserPOJO;
import ServicesAccessData.UserAdminPOJO;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.*;
import org.apache.thrift.TException;

/**
 *
 * @author node1
 */
public class UserDAO {

    public static ByteBuffer colParentFavorite = ByteBuffer.wrap("FavoriteStatus".getBytes());
    public static ByteBuffer colParentUser = ByteBuffer.wrap("User".getBytes());
    public static ByteBuffer colNameBanned = ByteBuffer.wrap("banned".getBytes());
    public static ByteBuffer colNameAdmin = ByteBuffer.wrap("admin".getBytes());
    public static ByteBuffer colNamePassWord = ByteBuffer.wrap("passWord".getBytes());    
    public static ByteBuffer colNameCreateDate = ByteBuffer.wrap("createDate".getBytes());
    public static ByteBuffer colNameModifyDate = ByteBuffer.wrap("modifyDate".getBytes());
    public static String colParentFavoriteS = "FavoriteStatus";
    public static String colParentUserS = "User";
    public static String colNameBannedS = "banned";
    public static String colNameAdminS = "admin";
    public static String colNamePassWordS = "passWord";
    public static String colNameCreateDateS = "createDate";
    public static String colNameModifyDateS = "modifyDate";
    private static Cassandra.Client client;
    private static UserDAO instance;

    public static UserDAO getInstance(Cassandra.Client _client) {
        if (instance != null) {
            return instance;
        }
        instance = new UserDAO();
        client = _client;
        return instance;
    }

    public boolean isAdminUser(String userID) {
        boolean result = false;
        try {
            ByteBuffer id = Common.GeneralHandling.toByteBuffer(userID);
            ColumnPath path = new ColumnPath(colParentUserS);
            path.column = colNameAdmin;
            ColumnOrSuperColumn colOrSupCol = client.get(id, path, ConsistencyLevel.ONE);
            String state = Common.GeneralHandling.toString(colOrSupCol.column.value);
            if (state.equals("1")) {
                result = true;
            }
        } catch (InvalidRequestException ex) {
            return result;
        } catch (NotFoundException ex) {
            return result;
        } catch (UnavailableException ex) {
            return result;
        } catch (TimedOutException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public void createCache(List<String> listMem) {
        if (!cachedAllUserBanned(listMem)) {
            System.out.println("Không thể tạo cache");
        }
        if (!cachedAllUserAdmin(listMem)) {
            System.out.println("Không thể tạo cache");
        }
    }

    public boolean updateIsAdminOnAUser(String userID, boolean state) {
        boolean result = false;
        try {
            ColumnParent parent = new ColumnParent(colParentUserS);
            ByteBuffer user_ID = Common.GeneralHandling.toByteBuffer(userID);
            String isPublic = (state == true) ? "1" : "0";
            client.insert(user_ID, parent, createCol(colNameAdmin, isPublic), Common.Constants.CL);
            client.insert(user_ID, parent, createCol(colNameCreateDate, ""), Common.Constants.CL);
            result = true;
        } catch (InvalidRequestException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnavailableException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public boolean updateIsAdminOnListUser(List<String> listUserID, boolean state) {
        boolean result = false;
        int numberUser = listUserID.size();
        for (int i = 0; i < numberUser; i++) {
            try {
                ColumnParent parent = new ColumnParent(colParentUserS);
                ByteBuffer user_ID = Common.GeneralHandling.toByteBuffer(listUserID.get(i));
                String isPublic = (state == true) ? "1" : "0";
                client.insert(user_ID, parent, createCol(colNameAdmin, isPublic), Common.Constants.CL);
                client.insert(user_ID, parent, createCol(colNameCreateDate, ""), Common.Constants.CL);
                result = true;
            } catch (InvalidRequestException ex) {
                Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnavailableException ex) {
                Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TimedOutException ex) {
                Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TException ex) {
                Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return result;
    }

    public List<UserPOJO> selectListAdminUser() {
        List<UserPOJO> listUser = new ArrayList<UserPOJO>();
        try {
            ColumnParent parent = new ColumnParent(colParentUserS);
            SlicePredicate predicate = new SlicePredicate();
            predicate.addToColumn_names(colNameCreateDate);
            predicate.addToColumn_names(colNameAdmin);
            predicate.addToColumn_names(colNameBanned);

            KeyRange range = new KeyRange();
            range.start_key = Common.GeneralHandling.toByteBuffer("");
            range.end_key = Common.GeneralHandling.toByteBuffer("");
            range.count = Short.MAX_VALUE;
            List<KeySlice> listKeySlice = client.get_range_slices(parent, predicate, range, Common.Constants.CL);
            listUser = getListUserFromListKeySliceAdmin(listKeySlice);
        } catch (UnavailableException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidRequestException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listUser;
    }

    public boolean cachedAllUserAdmin(List<String> listMem) {
        boolean result = false;
        List<UserPOJO> listUser = new ArrayList<UserPOJO>();
        try {
            ColumnParent parent = new ColumnParent(colParentUserS);
            SlicePredicate predicate = new SlicePredicate();
            predicate.addToColumn_names(colNameCreateDate);
            predicate.addToColumn_names(colNameAdmin);
            predicate.addToColumn_names(colNameBanned);

            KeyRange range = new KeyRange();
            range.start_key = Common.GeneralHandling.toByteBuffer("");
            range.end_key = Common.GeneralHandling.toByteBuffer("");
            range.count = Short.MAX_VALUE;
            List<KeySlice> listKeySlice = client.get_range_slices(parent, predicate, range, Common.Constants.CL);
            listUser = getListUserFromListKeySliceAdmin(listKeySlice);
            for (UserPOJO user : listUser) {
                Memcached.set("UserAdmin-" + user.userID, Common.Constants.EXPIRE, "1");
                listMem.add("UserAdmin-" + user.userID);
            }
            result = true;
        } catch (UnavailableException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidRequestException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public UserPOJO selectAllInfoOfAUser(String userID) {
        UserPOJO user = new UserPOJO();
        user.userID = userID;
        try {
            ByteBuffer keyRow = GeneralHandling.toByteBuffer(userID);
            SlicePredicate predicate = new SlicePredicate();
            predicate.addToColumn_names(colNameAdmin);
            predicate.addToColumn_names(colNameBanned);
            predicate.addToColumn_names(colNameCreateDate);
            ColumnParent parent = new ColumnParent(colParentUserS);
            List<ColumnOrSuperColumn> listColOrSupCol = client.get_slice(keyRow, parent, predicate, Common.Constants.CL);
            if (!listColOrSupCol.isEmpty()) {
                user = createUserPOJOFromListColOrSupCol(userID, listColOrSupCol);
            } else {
                user.userID = "-1";
            }

        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(CounterDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidRequestException ex) {
            Logger.getLogger(CounterDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnavailableException ex) {
            Logger.getLogger(CounterDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(CounterDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(CounterDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return user;
    }

    public boolean isBannedUser(String userID) {
        boolean result = false;
        try {
            ByteBuffer id = Common.GeneralHandling.toByteBuffer(userID);
            ColumnPath path = new ColumnPath(colNameBannedS);
            path.column = colNameBanned;
            ColumnOrSuperColumn colOrSupCol = client.get(id, path, ConsistencyLevel.ONE);
            String state = Common.GeneralHandling.toString(colOrSupCol.column.value);
            if (state.equals("1")) {
                result = true;
            }
        } catch (InvalidRequestException ex) {
            return result;
        } catch (NotFoundException ex) {
            return result;
        } catch (UnavailableException ex) {
            return result;
        } catch (TimedOutException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public List<StatusPOJO> selectFavoriteStatus(String userID) {
        List<StatusPOJO> listStatus = new ArrayList<StatusPOJO>();
        try {
            ByteBuffer id = Common.GeneralHandling.toByteBuffer(userID);
            ColumnParent parent = new ColumnParent(colParentFavoriteS);
            SlicePredicate predicate = new SlicePredicate();
            SliceRange range = new SliceRange();
            range.start = Common.GeneralHandling.toByteBuffer("");
            range.finish = Common.GeneralHandling.toByteBuffer("");
            range.count = 200;
            predicate.slice_range = range;
            List<ColumnOrSuperColumn> listColOrSupCol = client.get_slice(id, parent, predicate, Common.Constants.CL);
            List<ByteBuffer> listStatusID = new ArrayList<ByteBuffer>();
            for (ColumnOrSuperColumn colOrSupCol : listColOrSupCol) {
                if (Common.GeneralHandling.toString(colOrSupCol.column.value).equals("1")) {
                    listStatusID.add(colOrSupCol.column.name);
                }
            }
            listStatus = StatusDAO.getListStatusFromListStatusIDV1(listStatusID, client);
        } catch (UnavailableException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidRequestException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listStatus;
    }

    public boolean updateIsBannedOnAUser(String userID, boolean state) {
        boolean result = false;
        try {
            ColumnParent parent = new ColumnParent(colParentUserS);
            ByteBuffer userid = Common.GeneralHandling.toByteBuffer(userID);
            String isPublic = (state == true) ? "1" : "0";
            client.insert(userid, parent, createCol(colNameBanned, isPublic), Common.Constants.CL);
            client.insert(userid, parent, createCol(colNameCreateDate, ""), Common.Constants.CL);
            result = true;
        } catch (InvalidRequestException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnavailableException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public int updateIsBannedOnListUser(List<String> listUserID, boolean state) {
        int result = 0;
        for (String userID : listUserID) {
            try {
                ColumnParent parent = new ColumnParent(colParentUserS);
                ByteBuffer userid = Common.GeneralHandling.toByteBuffer(userID);
                String isPublic = (state == true) ? "1" : "0";
                client.insert(userid, parent, createCol(colNameBanned, isPublic), Common.Constants.CL);
                client.insert(userid, parent, createCol(colNameCreateDate, ""), Common.Constants.CL);
                result++;
            } catch (InvalidRequestException ex) {
                Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnavailableException ex) {
                Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TimedOutException ex) {
                Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TException ex) {
                Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return result;
    }

    public boolean insertFavoriteStatus(String userID, String statusID) {
        boolean result = false;
        try {
            ColumnParent parent = new ColumnParent(colParentFavoriteS);
            ByteBuffer userid = Common.GeneralHandling.toByteBuffer(userID);
            client.insert(userid, parent, createCol(Common.GeneralHandling.toByteBuffer(statusID), "1"), Common.Constants.CL);
            result = true;
        } catch (InvalidRequestException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnavailableException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public boolean removeFavoriteStatus(String userID, String statusID) {
        boolean result = false;
        try {
            ColumnParent parent = new ColumnParent(colParentFavoriteS);
            ByteBuffer userid = Common.GeneralHandling.toByteBuffer(userID);
            client.insert(userid, parent, createCol(Common.GeneralHandling.toByteBuffer(statusID), "0"), Common.Constants.CL);
            result = true;
        } catch (InvalidRequestException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnavailableException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public List<UserPOJO> selectAllUserBanned() {
        List<UserPOJO> listUser = new ArrayList<UserPOJO>();
        try {
            ColumnParent parent = new ColumnParent(colParentUserS);
            SlicePredicate predicate = new SlicePredicate();
            predicate.addToColumn_names(colNameCreateDate);
            predicate.addToColumn_names(colNameAdmin);
            predicate.addToColumn_names(colNameBanned);

            KeyRange range = new KeyRange();
            range.start_key = Common.GeneralHandling.toByteBuffer("");
            range.end_key = Common.GeneralHandling.toByteBuffer("");
            range.count = Short.MAX_VALUE;
            List<KeySlice> listKeySlice = client.get_range_slices(parent, predicate, range, Common.Constants.CL);
            listUser = getListUserFromListKeySliceBanned(listKeySlice);

            Memcached.set("AllUserBanned", Common.Constants.EXPIRE, listUser);

        } catch (UnavailableException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidRequestException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listUser;
    }

    public boolean cachedAllUserBanned(List<String> listMem) {
        boolean result = false;
        List<UserPOJO> listUser = new ArrayList<UserPOJO>();
        try {
            ColumnParent parent = new ColumnParent(colParentUserS);
            SlicePredicate predicate = new SlicePredicate();
            predicate.addToColumn_names(colNameCreateDate);
            predicate.addToColumn_names(colNameAdmin);
            predicate.addToColumn_names(colNameBanned);

            KeyRange range = new KeyRange();
            range.start_key = Common.GeneralHandling.toByteBuffer("");
            range.end_key = Common.GeneralHandling.toByteBuffer("");
            range.count = Short.MAX_VALUE;
            List<KeySlice> listKeySlice = client.get_range_slices(parent, predicate, range, Common.Constants.CL);
            listUser = getListUserFromListKeySliceBanned(listKeySlice);
            for (UserPOJO user : listUser) {
                Memcached.set("UserBanned-" + user.userID, Common.Constants.EXPIRE, "1");
                listMem.add("UserBanned-" + user.userID);
            }
            result = true;
        } catch (UnavailableException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidRequestException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public List<String> getListPagingUser(int limit) {
        List<String> listPagingUser = new ArrayList<String>();
        try {
            ColumnParent parent = new ColumnParent(colParentUserS);

            SlicePredicate predicate = new SlicePredicate();
            predicate.addToColumn_names(colNameCreateDate);
            KeyRange range = new KeyRange();
            range.start_key = GeneralHandling.toByteBuffer("");
            range.end_key = GeneralHandling.toByteBuffer("");
            range.count = Short.MAX_VALUE;

            List<KeySlice> listKeySlice = client.get_range_slices(parent, predicate, range, Common.Constants.CL);
            for (int i = 0; i < listKeySlice.size();) {
                listPagingUser.add(Common.GeneralHandling.toString(listKeySlice.get(i).key));
                i += limit;
            }
        } catch (InvalidRequestException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnavailableException ex) {
        } catch (TimedOutException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listPagingUser;
    }

    public List<UserPOJO> selectListUserFromPage(String startKey, int limit) {
        List<UserPOJO> listUser = new ArrayList<UserPOJO>();
        try {
            ColumnParent parent = new ColumnParent(colParentUserS);
            SlicePredicate predicate = new SlicePredicate();
            predicate.addToColumn_names(colNameCreateDate);
            predicate.addToColumn_names(colNameAdmin);
            predicate.addToColumn_names(colNameBanned);

            KeyRange range = new KeyRange();
            range.start_key = Common.GeneralHandling.toByteBuffer(startKey);
            range.end_key = Common.GeneralHandling.toByteBuffer("");
            range.count = limit;
            List<KeySlice> listKeySlice = client.get_range_slices(parent, predicate, range, Common.Constants.CL);
            listUser = getListUserFromListKeySlice(listKeySlice);
        } catch (InvalidRequestException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnavailableException ex) {
        } catch (TimedOutException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listUser;
    }

    public Column createCol(ByteBuffer colName, String value) {
        Column result = new Column();
        long time = System.currentTimeMillis();
        try {
            result = new Column(colName);
            if ((colName.equals(colNameCreateDate) || colName.equals(colNameModifyDate) && "".equals(value))) {
                Calendar cal = Calendar.getInstance();
                SimpleDateFormat now = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
                result.setValue(Common.GeneralHandling.toByteBuffer(now.format(cal.getTime())));
            } else {
                result.setValue(Common.GeneralHandling.toByteBuffer(value));
            }
            result.setTimestamp(time);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(GeneralHandling.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public List<UserPOJO> getListUserFromListKeySlice(List<KeySlice> listKeySlice) {
        List<UserPOJO> listUser = new ArrayList<UserPOJO>();
        try {
            for (int i = 0; i < listKeySlice.size(); i++) {
                List<ColumnOrSuperColumn> listColOrSupCol = listKeySlice.get(i).columns;
                String userID = Common.GeneralHandling.toString(listKeySlice.get(i).key);
                UserPOJO user = createUserPOJOFromListColOrSupCol(userID, listColOrSupCol);
                listUser.add(user);
            }
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listUser;
    }

    public List<UserPOJO> getListUserFromListKeySliceBanned(List<KeySlice> listKeySlice) {
        List<UserPOJO> listUser = new ArrayList<UserPOJO>();
        try {
            for (int i = 0; i < listKeySlice.size(); i++) {
                List<ColumnOrSuperColumn> listColOrSupCol = listKeySlice.get(i).columns;
                String userID = Common.GeneralHandling.toString(listKeySlice.get(i).key);
                UserPOJO user = createUserPOJOFromListColOrSupCol(userID, listColOrSupCol);
                if (user.isBanned) {
                    listUser.add(user);
                }
            }
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listUser;
    }

    public List<UserPOJO> getListUserFromListKeySliceAdmin(List<KeySlice> listKeySlice) {
        List<UserPOJO> listUser = new ArrayList<UserPOJO>();
        try {
            for (int i = 0; i < listKeySlice.size(); i++) {
                List<ColumnOrSuperColumn> listColOrSupCol = listKeySlice.get(i).columns;
                String userID = Common.GeneralHandling.toString(listKeySlice.get(i).key);
                UserPOJO user = createUserPOJOFromListColOrSupCol(userID, listColOrSupCol);
                if (user.isAdmin == true) {
                    listUser.add(user);
                }
            }
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listUser;
    }

    private UserPOJO createUserPOJOFromListColOrSupCol(String userID, List<ColumnOrSuperColumn> listColOrSupCol) {
        UserPOJO user = new UserPOJO();
        user.userID = userID;
        for (int j = 0; j < listColOrSupCol.size(); j++) {
            try {
                ColumnOrSuperColumn colOrSupCol = listColOrSupCol.get(j);
                Column col = colOrSupCol.column;
                ByteBuffer temp = col.name;
                String colName = Common.GeneralHandling.toString(temp);
                if (colName.equals(colNameBannedS)) {
                    user.isBanned = ("1".equals(Common.GeneralHandling.toString(col.value))) ? true : false;
                }
                if (colName.equals(colNameAdminS)) {
                    user.isAdmin = ("1".equals(Common.GeneralHandling.toString(col.value))) ? true : false;
                }
                if (colName.equals(colNameCreateDateS)) {
                    user.createDate = Common.GeneralHandling.toString(col.value);
                }
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return user;
    }

    private UserAdminPOJO createUserAdminPOJOFromListColOrSupCol(String userID, List<ColumnOrSuperColumn> listColOrSupCol) {
        UserAdminPOJO user = new UserAdminPOJO();
        user.userID = userID;
        user.passWord = "-1";
        for (int j = 0; j < listColOrSupCol.size(); j++) {
            try {
                ColumnOrSuperColumn colOrSupCol = listColOrSupCol.get(j);
                Column col = colOrSupCol.column;
                ByteBuffer temp = col.name;
                if (temp.equals(colNamePassWord)) {
                    user.passWord = GeneralHandling.toString(col.value);
                }
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return user;
    }    
    
    
    public boolean setUserAdmin(String userID, String passWord){
        boolean result = false;
        try {
            ColumnParent parent = new ColumnParent(colParentUserS);
            ByteBuffer userid = Common.GeneralHandling.toByteBuffer(userID);
            client.insert(userid, parent, createCol(colNamePassWord, passWord), Common.Constants.CL);
            result = true;
        } catch (InvalidRequestException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnavailableException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }


    public UserAdminPOJO getUserAdmin(String userID){
        UserAdminPOJO userAdmin = new UserAdminPOJO();
        userAdmin.userID = userID;
        try {
            ByteBuffer keyRow = GeneralHandling.toByteBuffer(userID);
            SlicePredicate predicate = new SlicePredicate();
            predicate.addToColumn_names(colNamePassWord);
            ColumnParent parent = new ColumnParent(colParentUserS);
            List<ColumnOrSuperColumn> listColOrSupCol = client.get_slice(keyRow, parent, predicate, Common.Constants.CL);
            if (!listColOrSupCol.isEmpty()) {
                userAdmin = createUserAdminPOJOFromListColOrSupCol(userID, listColOrSupCol);
            } else {
                userAdmin.userID = "-1";
                userAdmin.passWord = "-1";
            }

        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(CounterDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidRequestException ex) {
            Logger.getLogger(CounterDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnavailableException ex) {
            Logger.getLogger(CounterDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(CounterDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(CounterDAO.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return userAdmin;
    }


    public List<UserAdminPOJO> getListUserAdminPOJO(){
        List<UserAdminPOJO> listUserAdmin = new ArrayList<UserAdminPOJO>();
        try {
            ColumnParent parent = new ColumnParent(colParentUserS);
            SlicePredicate predicate = new SlicePredicate();
            predicate.addToColumn_names(colNamePassWord);

            KeyRange range = new KeyRange();
            range.start_key = Common.GeneralHandling.toByteBuffer("");
            range.end_key = Common.GeneralHandling.toByteBuffer("");
            range.count = Short.MAX_VALUE;
            List<KeySlice> listKeySlice = client.get_range_slices(parent, predicate, range, Common.Constants.CL);
            listUserAdmin = getListUserAdminPOJOFromListKeySlice(listKeySlice);
        } catch (UnavailableException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidRequestException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        }       
        return listUserAdmin;
    }


    public boolean removeUserAdmin(String userID){
        boolean result = false;
        try {
            ByteBuffer rowKey = GeneralHandling.toByteBuffer(userID);
            ColumnPath path = new ColumnPath(colParentUserS);
            ColumnParent parent = new ColumnParent(colParentUserS);
            SlicePredicate predicate = new SlicePredicate();
            long timestamp = System.currentTimeMillis();
            client.remove(rowKey, path, timestamp, ConsistencyLevel.ONE);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidRequestException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnavailableException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    private List<UserAdminPOJO> getListUserAdminPOJOFromListKeySlice(List<KeySlice> listKeySlice) {
        List<UserAdminPOJO> listUserAdmin = new ArrayList<UserAdminPOJO>();
        try {
            for (int i = 0; i < listKeySlice.size(); i++) {
                List<ColumnOrSuperColumn> listColOrSupCol = listKeySlice.get(i).columns;
                String userID = Common.GeneralHandling.toString(listKeySlice.get(i).key);
                UserAdminPOJO user = createUserAdminPOJOFromListColOrSupCol(userID, listColOrSupCol);
                if(!"-1".equals(user.passWord)){
                    listUserAdmin.add(user);
                }
            }
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listUserAdmin;
    }


}